package com.example.jegotrip.activity;

import static android.view.View.GONE;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toolbar;

import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.example.jegotrip.R;
import com.example.jegotrip.adapter.CartListAdapter;
import com.example.jegotrip.base.BaseActivity;
import com.example.jegotrip.db.CartDBHelper;
import com.example.jegotrip.entity.CartInfo;
import com.example.jegotrip.entity.UserInfo;
import com.example.jegotrip.utils.CustomToast;

import java.util.List;

public class ShoppingCartActivity extends BaseActivity {
    private Toolbar toolbar;
    private UserInfo userInfo;
    private CartInfo cartInfo;
    private List<CartInfo> cartInfos;
    private String[] cartIds;
    private RecyclerView cart_recyclerView;
    private ImageView cart_product_img;
    private TextView cart_product_check;
    private boolean cart_is_checked;
    private TextView cart_product_title;
    private TextView cart_product_price;
    //    适配器
    private CartListAdapter cartListAdapter;
    private LinearLayoutCompat ll_cart_empty;
    private Button add_order;
    private String[] item = new String[]{"详情","编辑","删除"};
    private int currentIndex = 0;
    @Override
    protected void initData() {
//        加载购物车数据
        userInfo = UserInfo.getUserInfo();
        cartInfos = CartDBHelper.getInstance(ShoppingCartActivity.this).queryCarts();
//        初始化适配器
        cartListAdapter = new CartListAdapter(cartInfos);
        if (cartInfos.isEmpty()){
            ll_cart_empty.setVisibility(View.VISIBLE);
        } else {
            ll_cart_empty.setVisibility(GONE);
        }

//        设置适配器
        cart_recyclerView.setAdapter(cartListAdapter);

//        recyclerView点击事件
        cartListAdapter.setmCartListOnItemClickListener(new CartListAdapter.CartListOnItemClickListener() {
            @Override
            public void onCartItemClick(int position, CartInfo cartInfo) {

            }

            @Override
            public void onCartMoreItemClick(int position, CartInfo cartInfo) {
                new AlertDialog.Builder(ShoppingCartActivity.this)
                        .setTitle("操作")
                        .setSingleChoiceItems(item, 0, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                                currentIndex = i;
                            }
                        })
                        .setNegativeButton("取消",null)
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                                if (currentIndex == 0){// 详情

                                } else if (currentIndex == 1){// 编辑

                                } else if (currentIndex == 2){
                                    int res = CartDBHelper.getInstance(ShoppingCartActivity.this).deleteCart(cartInfo.getCart_id());
                                    if (res > 0){
                                        CustomToast.showCenterToast(ShoppingCartActivity.this,"删除成功");
                                        initData();
                                    } else {
                                        CustomToast.showCenterToast(ShoppingCartActivity.this,"操作失败");

                                    }
                                }
                            }
                        })
                        .show();
            }

            @Override
            public void onCartAddOrderMoreItemClick(int position, CartInfo cartInfo) {
                if (!isContains(cartInfo.getCart_id()+"")){

                }
            }
        });
    }

    private boolean isContains(String cardId){
        boolean contains = false;

        for (int i = 0; i < cartIds.length; i++) {
            if (cartIds[i] == cardId) {
                contains = true;
                break; // 找到匹配元素后跳出循环
            }
        }
        return contains;
    }

    @Override
    protected void initViews() {
        toolbar = findViewById(R.id.toolBar);

        cart_recyclerView = findViewById(R.id.cart_recyclerView);

        ll_cart_empty = findViewById(R.id.ll_cart_empty);

        add_order = findViewById(R.id.add_order);

    }

    @Override
    protected int getLayoutResOId() {
        return R.layout.activity_shopping_cart;
    }

    @Override
    protected void setListener() {
        toolbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        add_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                loadingDialog.showLoading("加载中...");
//                // 模拟一个耗时操作
//                new Handler().postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        loadingDialog.hideLoading();
//                    }
//                }, 3000);
                CustomToast.showCenterToast(ShoppingCartActivity.this,"下单成功");
                finish();
            }
        });
    }
}
